#!/usr/bin/env php
<?php
// parse_doc.php for  in /Users/lyos2210/Desktop/cours_piscine/php/exo_php/imagepanel
// 
// Made by Abdullah YASSIR HOUSSEN
// Login   <yassir_a@etna-alternance.net>
// 
// Started on  Thu Nov 15 19:23:48 2012 Abdullah YASSIR HOUSSEN
// Last update Tue Dec 11 20:51:33 2012 Abdullah YASSIR HOUSSEN
//

error_reporting(E_ALL);

function bluebird($param)
{
      if (($str = file_get_contents($param)) !== false)
	return $str;
      else
	echo "image panel: $param: Permission denied\n";
}

/** recupere un le contneu d un page source html contenant l ensemble des
 ** liens pour recuperer sur le serveur.
 ** retourne un tableau contenant les addresse des differents
 ** elements a telecharger ici des images;
 **/
function parse($str, $urlname)
{
  $tab = array();
  $p = "#^.*\bsrc=\"#";
   $pattern = "#<img\s*(\w*)[^>]+src=\s*([\"\'][^\"\']+[|\"\']|(.*)+)[^\/>]#U";
  preg_match_all($pattern, $str, $matches);
  $j = 0;
  while (isset($matches[0][$j]))
    {
      $url = strpbrk($matches[0][$j], 'src=');
      $url = substr($url, 0, -2);
      $url = preg_replace($p , "", $url);
      if (substr($url,0,7) == "http://" || substr($url,0,3) == "www")
	$tab[$j] = $url;
      else
	if (substr($url,0,1) == "/")
	  $tab[$j] = $urlname.''.$url;
	else
	  $tab[$j] =$urlname.'/'.$url;
      $j++;
    }
  return ($tab);
}

function rm_file_ext($dir, $ext)
{
  $repertoire = opendir($dir);
  while(false !== ($fichier = readdir($repertoire)))
    {
      $chemin = $dir."/".$fichier;
      $infos = pathinfo($chemin);
      $extension = $infos['extension'];
      if($extension == $ext)
	unlink($chemin);
    }
  closedir($repertoire);
}

function download(&$tab_recu, $structure, $urlname)
{
  $max_dl = 200;
  $i = 0;
  $tab_renvoie = array();
  $count = count($tab_recu);
  $value = ($count > $max_dl)? $max_dl : $count;
  while($i < $value)
    {
      $url = $tab_recu[$i];
      $extension = substr($url, strlen($url) - 3 ,strlen($url));
      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $data = curl_exec($ch);
      curl_close($ch);
      file_put_contents($structure."$i.$urlname.$extension", $data);
      $i++;
    }
  // rm_file_ext($structure, "png");
  rm_file_ext(".", "png");
  $k = 0;
  $structure = ".";
  if($toto = opendir($structure))
    while ($file = readdir($toto))
      {
	if ($file != "." && $file != "..")
	  {
	    $tab_renvoie[$k] = $file;
	    $k++;
	  }
      }
  closedir($toto);
  return ($tab_renvoie);
}

/**main du script
 **creer un dossier de nomme /temp_img_$i/
 **dans le dossier courant
 **recupere l url passe en parametre
 **/
echo "bibliotheque de test : les images des archives de bonjour madame \n";
$this_toto = "http://www.bonjourmadame.fr/archive";
echo "$this_toto \n";
  $patt_url = "#http(s)*\:\/\/(\w)*(\.[\w-]*)*(\.[a-z]{2,4})\/#i";
  preg_match_all($patt_url, $this_toto, $toto);
  $url = substr($toto[0][0], 0, -1);
  $urlname = substr($toto[3][0], 1);
  $structure = "";
// if (!mkdir($structure, 0777, true))
//   echo ("imagepanel.php: create directory: directory exist\n");
  $parse = parse(bluebird($this_toto), $url);
  $tutu = download($parse, $structure, $urlname);
  return ($tutu);
?>